//
//  ZZYPhotoHelper.m
//  

#import "ZZYPhotoHelper.h"

#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>

#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>


@interface ZZYPhotoDelegateHelper: NSObject<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, copy) ZZYPhotoHelperBlock selectImageBlock;
@end

@interface ZZYPhotoHelper ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic) ZZYPhotoDelegateHelper *helper;

@end

static ZZYPhotoHelper *picker = nil;

@implementation ZZYPhotoHelper

+ (instancetype)shareHelper{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        picker = [[ZZYPhotoHelper alloc] init];
    });
    return picker;
}

- (void)showImageViewSelcteWithResultBlock:(ZZYPhotoHelperBlock)selectImageBlock withOnlyCamera:(BOOL)camera andDefaultFront:(BOOL)isFront{
    if (camera == YES) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [self creatWithSourceType:UIImagePickerControllerSourceTypeCamera block:selectImageBlock withFront:isFront];
        }
        else{
            [CreaditUtilsHelper boxShowWithMessage:@"camera can not use"];
        }
        
    }
    else{
        [self creatWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary block:selectImageBlock withFront:isFront];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    if ([picker isKindOfClass:[UIImagePickerController class]]) {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)creatWithSourceType:(UIImagePickerControllerSourceType)sourceType block:selectImageBlock withFront:(BOOL)isFront{
    picker.helper                  = [[ZZYPhotoDelegateHelper alloc] init];
    picker.delegate                = picker.helper;
    picker.sourceType              = sourceType;
    picker.allowsEditing           = NO;
    
    picker.helper.selectImageBlock = selectImageBlock;
    
    if (isFront) {
        picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self animated:YES completion:nil];
}

@end


@implementation ZZYPhotoDelegateHelper

#pragma mark - UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *theImage = nil;
    if ([picker allowsEditing]){
        theImage = [info objectForKey:UIImagePickerControllerEditedImage];
    } else {
        theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }

    if (_selectImageBlock) {
        _selectImageBlock(theImage);
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}



@end
